package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import cn.bingoogolapple.bgabanner.BGABanner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BasePageFragment
import com.hunktimes.wholesale.databinding.FragmentProductBinding
import com.hunktimes.wholesale.extension.setImageUrl
import com.hunktimes.wholesale.mock.bannerWatsons
import com.hunktimes.wholesale.mock.comments
import com.hunktimes.wholesale.mock.goods
import com.hunktimes.wholesale.net.model.BannerResp
import com.hunktimes.wholesale.net.model.MockComment
import com.hunktimes.wholesale.net.model.MockGoods
import com.hunktimes.wholesale.ui.tabs.dashboard.CartRecommendAdapter
import com.hunktimes.wholesale.util.Constants

/**
 * Author: Delete
 * Date: 2020/7/27 17:36
 * Description:
 *
 */
class ProductFragment : BasePageFragment(R.layout.fragment_product) {
    lateinit var viewBinding: FragmentProductBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentProductBinding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        //initbanner()
        initCover()
        initComments()
        initRecommend()
    }

    /**
     * 初始化顶部图片
     */
    fun initCover() {
        val mockGoods = arguments?.getParcelable<MockGoods>(Constants.key_goods_bundle)
        mockGoods?.let {
            viewBinding.coverIv.setImageUrl(it.imageUrl)
            viewBinding.productTitleTv.text = it.name
            viewBinding.productDescTv.text = it.desc
            viewBinding.productPriceTv.text = it.price
        }
    }

    /**
     * banner 配置
     */
    val bannerAdapter = object : BGABanner.Adapter<ImageView, BannerResp> {
        override fun fillBannerItem(
            banner: BGABanner?,
            itemView: ImageView?,
            model: BannerResp?,
            position: Int
        ) {
            itemView?.setImageUrl(model?.img!!)
        }

    }

    fun initbanner() {
//        val data = arrayListOf<BannerResp>(
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbc43be1.jpg"),
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbdb09dd.jpg"),
//            BannerResp("http://pic1.win4000.com/wallpaper/8/583fbfbf301e4.jpg")
//        )
        val data = bannerWatsons.map { BannerResp(it) }

//        viewBinding.banner.apply {
//            setAdapter(bannerAdapter)
//            setData(data, null)
//            viewBinding.banner.setDelegate { _, _, bannerResp, _ ->
//                L.d("Banner click -> $bannerResp")
//            }
//        }

    }

    val commentAdapter = CommentAdapter()
    fun initComments() {
        viewBinding.productCommentHeader.setOnClickListener {
            rootNavController.navigate(R.id.action_navigation_product_to_navigation_comments)
        }

        val commentLm = LinearLayoutManager(requireContext())
        commentLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.productCommentRcv.layoutManager = commentLm
        viewBinding.productCommentRcv.adapter = commentAdapter

        val data = mutableListOf<MockComment>()
//        for (i in 1..3) {
//            data.add("購物車推薦$i")
//        }
        repeat(3) {
            data.add(comments.random())
        }
        commentAdapter.data = data
    }

    val recommendAdapter = CartRecommendAdapter()
    fun initRecommend() {
        val recommendLLm = GridLayoutManager(requireContext(), 2)
        recommendLLm.orientation = GridLayoutManager.VERTICAL
        viewBinding.productRecommentRcv.layoutManager = recommendLLm
        viewBinding.productRecommentRcv.adapter = recommendAdapter
        val data = mutableListOf<MockGoods>()
//        for (i in 1..3) {
//            data.add("購物車推薦$i")
//        }
        repeat(3) {
            data.add(goods.random())
        }
        recommendAdapter.data = data
    }
}

class CommentAdapter : BaseQuickAdapter<MockComment, BaseViewHolder>(R.layout.item_comment) {
    override fun convert(holder: BaseViewHolder, item: MockComment) {
        holder.setText(R.id.item_comment_username_tv, item.username)
        holder.setText(R.id.item_comment_date_tv, item.date)
        holder.setText(R.id.item_comment_tv, item.comment)

    }
}